Skip to content

feat(node): Only setup orchestrion channel listeners when needed#22312

Draft
mydea wants to merge 10 commits into
developfrom
fn/lazy-load-channels
Draft

feat(node): Only setup orchestrion channel listeners when needed#22312
mydea wants to merge 10 commits into
developfrom
fn/lazy-load-channels

Conversation

@mydea

@mydea mydea commented Jul 16, 2026

Copy link
Copy Markdown
Member

This PR implements a way to lazy-load orchestrion instrumentation (runtime code) only when something was actually wrapped.

  • in setupOnce we look at the list of instrumented modules, and only run the instrumentation if something was already orchestrion-wrapped
  • in setup we add a listener to a client hook event which is emitted whenever something is runtime-wrapped by orchestrion. There we check if a module was wrapped that we need, and if so, we add the instrumentation, listening to the respective channels.
  • We keep a symbol on the callback function itself to keep track of if this was run or not, ensuring we only register stuff once.

Other Runtimes (Deno, Bun)

In Deno and Bun we do not have the concrete list of build-time instrumented things (yet). However, the limits of channel registrations also do not really apply there - so this PR just registers everything immediately there. We can revisit this later.

Next.js

This needs adjustments for turbopack...

Closes #22302


const BUNDLER_MARKER_BANNER =
';(globalThis.__SENTRY_ORCHESTRION__=(globalThis.__SENTRY_ORCHESTRION__||{})).bundler=true;';
';(globalThis.__SENTRY_ORCHESTRION__=(globalThis.__SENTRY_ORCHESTRION__||{})).bundler=[];';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @isaacs do we actually need this? Can we properly inject this with the correct modules somehow?

Comment thread packages/server-utils/src/integrations/tracing-channel/express/index.ts Outdated
Comment thread packages/server-utils/src/orchestrion/runtime/register.ts Outdated
Comment thread packages/server-utils/src/orchestrion/instrumentation.ts
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.74 kB - -
@sentry/browser - with treeshaking flags 26.18 kB - -
@sentry/browser (incl. Tracing) 46.55 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.35 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.36 kB - -
@sentry/browser (incl. Tracing, Replay) 85.81 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.46 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.52 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 103.2 kB - -
@sentry/browser (incl. Feedback) 44.93 kB - -
@sentry/browser (incl. sendFeedback) 32.54 kB - -
@sentry/browser (incl. FeedbackAsync) 37.68 kB - -
@sentry/browser (incl. Metrics) 28.83 kB - -
@sentry/browser (incl. Logs) 29.06 kB - -
@sentry/browser (incl. Metrics & Logs) 29.75 kB - -
@sentry/react 29.54 kB - -
@sentry/react (incl. Tracing) 48.81 kB - -
@sentry/vue 33.17 kB - -
@sentry/vue (incl. Tracing) 48.52 kB - -
@sentry/svelte 27.77 kB - -
CDN Bundle 30.14 kB - -
CDN Bundle (incl. Tracing) 48.52 kB - -
CDN Bundle (incl. Logs, Metrics) 31.73 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.82 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.97 kB - -
CDN Bundle (incl. Tracing, Replay) 86.03 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.34 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.83 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 93.09 kB - -
CDN Bundle - uncompressed 89.87 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.68 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.66 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.32 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.89 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.86 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.6 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.55 kB - -
@sentry/nextjs (client) 51.36 kB - -
@sentry/sveltekit (client) 46.98 kB - -
@sentry/core/server 78.69 kB - -
@sentry/core/browser 65.06 kB - -
@sentry/node-core 63.21 kB - -
@sentry/node 125.51 kB -0.01% -10 B 🔽
@sentry/node (incl. diagnostics channel injection) 148.41 kB +0.25% +356 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.03 kB -0.01% -2 B 🔽
@sentry/node/light 51.32 kB - -
@sentry/node - without tracing 74.71 kB - -
@sentry/aws-serverless 83.92 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 182.12 kB - -
@sentry/cloudflare (withSentry) 451.05 kB - -

View base workflow run

Comment thread packages/server-utils/src/integrations/tracing-channel/express/index.ts Outdated
Comment thread packages/server-utils/src/orchestrion/instrumentation.ts Outdated
Comment thread packages/server-utils/src/orchestrion/instrumentation.ts
Comment thread packages/server-utils/src/orchestrion/detect.ts
Comment thread packages/bun/src/plugin.ts
Comment thread packages/server-utils/src/orchestrion/instrumentation.ts
*/
export function getOrchestrionInjectedModules(): string[] {
const { runtime, bundler } = GLOBAL_OBJ.__SENTRY_ORCHESTRION__ ?? {};
return [...(runtime ?? []), ...(bundler ?? [])];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Do we care about potential duplicates? Probably not, as this is for now used anyways just for modules.includes(name)

export const fastifyIntegration = defineIntegration((options: Partial<FastifyIntegrationOptions> = {}) =>
_fastifyIntegration(options),
);
export const fastifyIntegration = defineIntegration(_fastifyIntegration);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Just for my understanding, not to miss anything. The Fastify changes are unrelated theoretically right?

Ok to land here, since they are small changes 🤏

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah just noticed this unnecessary wrapper here :)

@@ -0,0 +1,78 @@
import * as diagnosticsChannel from 'node:diagnostics_channel';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: The PR description states it was meant only for Express. Was Kafka and GraphQL intended to be in here too?

Implementation looks fine though

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description is inclomplete, I actually moved everything now :)

const subscribers: Partial<TracingChannelSubscribers<ConsumerRunChannelContext>> = {
start(ctx) {
const config = ctx.arguments[0];
if (!config || typeof config !== 'object') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: We could use the new isObjectLike helper

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not change this here, just moved it - we can/should update this in a follow up I'd say :)

Comment thread packages/server-utils/src/orchestrion/config/anthropic-ai.ts Outdated
@mydea
mydea force-pushed the fn/lazy-load-channels branch from acd44ec to cd1e0f9 Compare July 17, 2026 07:54
Comment thread packages/server-utils/src/orchestrion/instrumentation.ts
Comment thread packages/server-utils/src/integrations/tracing-channel/redis.ts

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 9b33e48. Configure here.

Comment thread packages/server-utils/src/integrations/tracing-channel/mysql2.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Work around node's hard limit of 1024 diagnostics channels

3 participants